package com.kinghood.authoritycenter.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;

import com.kinghood.authoritycenter.common.config.TokenProvider;
import com.kinghood.authoritycenter.common.model.constant.AuthConstant;
import com.kinghood.authoritycenter.common.model.dao.auth.OpenAccountDO;
import com.kinghood.authoritycenter.common.properties.BaseProperties;
import com.kinghood.authoritycenter.service.LoginService;
import com.kinghood.authoritycenter.common.model.dto.auth.KingHoodPrincipal;
import com.kinghood.authoritycenter.common.model.dto.auth.UserDTO;
import com.kinghood.redis.util.RedisManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2023-12-06 14:54
 **/
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    private final TokenProvider tokenProvider;
    private final BaseProperties baseProperties;
    private final RedisManager<String> redisManager;

    @Override
    public KingHoodPrincipal<UserDTO> login(UserDTO user) {

        int tokenValidityInSeconds = baseProperties.getTokenValidityInSeconds();
        String token = tokenProvider.getToken(AuthConstant.ISSUER_INNER,user.getId().toString(), user.getUsername(),
                JSON.toJSONString(user),"ROLE_SYS_USER", tokenValidityInSeconds);

        KingHoodPrincipal<UserDTO> principal = new KingHoodPrincipal<>();
        principal.setToken(token);
        principal.setTokenValidityInSeconds(tokenValidityInSeconds);
        principal.setIssuer(AuthConstant.ISSUER_INNER);
        principal.setAudience(user.getId().toString());
        principal.setSubject(user.getUsername());
        principal.setExpiration(DateUtil.offsetSecond(new Date(),tokenValidityInSeconds));
        principal.setExpire(tokenValidityInSeconds);

        principal.setInfo(user);

        return principal;
    }

    @Override
    public KingHoodPrincipal<OpenAccountDO> openToken(OpenAccountDO openAccountDO) {
        int tokenValidityInSeconds = baseProperties.getTokenValidityInSeconds();
        String token = tokenProvider.getToken(AuthConstant.ISSUER_OPEN,openAccountDO.getId().toString(), openAccountDO.getAppId(),
                JSON.toJSONString(openAccountDO),"ROLE_OPEN_USER", tokenValidityInSeconds);

        KingHoodPrincipal<OpenAccountDO> principal = new KingHoodPrincipal<>();
        principal.setToken(token);
        principal.setTokenValidityInSeconds(tokenValidityInSeconds);
        principal.setIssuer(AuthConstant.ISSUER_INNER);
        principal.setAudience(openAccountDO.getAppId());
        principal.setSubject(openAccountDO.getAppSecret());
        principal.setExpiration(DateUtil.offsetSecond(new Date(), tokenValidityInSeconds));
        principal.setExpire(tokenValidityInSeconds);

        principal.setInfo(openAccountDO);

        return principal;
    }

}
